package com.showsu.editor.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.showsu.editor.dao.TblVipOrderMapper;
import com.showsu.editor.entity.doo.VipGoods;
import com.showsu.editor.entity.doo.VipOrder;
import com.showsu.editor.entity.po.TblVipOrder;
import com.showsu.editor.entity.vo.request.RequestVO;
import com.showsu.editor.enums.PayStatusEnum;
import com.showsu.editor.enums.RowFlagEnum;
import com.showsu.editor.service.IVipOrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * @Author: William
 * @Description:
 * @Date: Created in 6:53 下午 2024/9/27
 * @Modified By:
 */
@Service
@Slf4j
public class VipOrderServiceImpl extends ServiceImpl<TblVipOrderMapper, TblVipOrder> implements IVipOrderService {

    @Autowired
    private TblVipOrderMapper tblVipOrderMapper;

    @Override
    public VipOrder save(Long userId, VipGoods vipGoods, RequestVO clientInfo) {
        if (userId == null || vipGoods == null) {
            return null;
        }
        TblVipOrder tblVipOrder = new TblVipOrder();
        //生成订单号
        int orderRandomNum = RandomUtil.randomInt(2, 8);
        String outOrderNo = new StringBuilder("hsxpc")
                //加10个字符随机码 混淆userId
                .append(RandomUtil.randomString(10 - orderRandomNum))
                .append(userId).append(RandomUtil.randomString(orderRandomNum))
                .append(DateUtil.format(LocalDateTime.now(), DatePattern.PURE_DATETIME_MS_PATTERN))
                .toString();
        tblVipOrder.setOrderTradeNo(outOrderNo);
        tblVipOrder.setUserId(userId);
        tblVipOrder.setTotalFee(vipGoods.getPrice());
        tblVipOrder.setGoodId(vipGoods.getVipLevel());
        tblVipOrder.setGoodName(vipGoods.getRemark());
        tblVipOrder.setGoodPrice(vipGoods.getPrice());
        tblVipOrder.setStatus(PayStatusEnum.Waiting_pay.getCode());
        tblVipOrder.setClientIp(clientInfo.getClientIp());
        tblVipOrder.setClientOs(clientInfo.getClientOs());
        tblVipOrder.setClientBrowser(clientInfo.getClientBrowser());
        tblVipOrder.setClientOrigin(clientInfo.getClientOrigin());
        tblVipOrder.setCreateTime(new Date());
        tblVipOrderMapper.insert(tblVipOrder);
        return BeanUtil.copyProperties(tblVipOrder, VipOrder.class);
    }

    @Override
    public VipOrder queryByOrderTradeNo(String orderTradeNo) {
        if (StringUtils.isBlank(orderTradeNo)) {
            return null;
        }
        TblVipOrder tblVipOrder = tblVipOrderMapper.selectOne(new QueryWrapper<TblVipOrder>().lambda()
                .eq(TblVipOrder::getOrderTradeNo, orderTradeNo)
                .eq(TblVipOrder::getFlag, RowFlagEnum.UnDelete.getCode())
                .last("limit 1"));
        return tblVipOrder == null ? null : BeanUtil.copyProperties(tblVipOrder, VipOrder.class);
    }

    @Override
    public int updateVipOrder(Long id, String tradeNo, String appId, Integer status, String remark) {
        if (id == null || StringUtils.isBlank(tradeNo) || status == null) {
            return 0;
        }
        TblVipOrder tblVipOrder = new TblVipOrder();
        tblVipOrder.setId(id);
        tblVipOrder.setTradeNo(tradeNo);
        tblVipOrder.setAppId(appId);
        tblVipOrder.setStatus(status);
        tblVipOrder.setRemark(remark);
        return tblVipOrderMapper.updateById(tblVipOrder);
    }
}
